clear all 
set more off 
set maxvar 15000 
clear matrix


    foreach vers in log rank {

        if "`vers'"=="log" {
            local dep "log_son_baseline"
            local indep "log_father_baseline"
        }
        
        if "`vers'"=="rank" {
            local dep "rank_son_baseline"
            local indep "rank_father_baseline"
        }
        
	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
        keep if baseline_sample==1
        
        file open Table_results using "$Mydirectory2/appendix_a/Table_estimates_bysex_byrace_`vers'.tex", write replace

        file write Table_results "\begin{tabularx}{\hsize}{@{\hskip\tabcolsep\extracolsep\fill} l c c c c c c c}" _n
        file write Table_results "\toprule" _n
        file write Table_results "\addlinespace[2ex]" _n    
        file write Table_results "& 1910s & 1920s & 1930s & 1940s & 1950s & 1960s & 1970s  \\" _n
        file write Table_results "\addlinespace[1ex]" _n
        file write Table_results "\midrule" _n
        
    * Results by sex
        file write Table_results "\addlinespace[1ex]" _n
        file write Table_results "\textit{By sex:} & & & & & & & \\" 
        file write Table_results "\addlinespace[2ex]" _n

    foreach z in 1 2 { 

        if "`z'"=="1" {
            file write Table_results "Men " 
        }
        if "`z'"=="2" {
            file write Table_results "Women " 
        }
        
            forval i=1910(10)1970 {

                reg `dep' `indep'  if decade==`i' & sex==`z' [pw=wgt_sex_race],   robust
                
                local coeff = _b[`indep']
                local number1: display %-09.2fc `coeff' 
                
                local numb = _se[`indep']
                local se_`i': display %-04.2fc `numb'
                display `se_`i''
                
                file write Table_results "& `number1' "

            }

            file write Table_results " \\" _n
            
            file write Table_results "& (`se_1910') & (`se_1920') & (`se_1930') & (`se_1940') & (`se_1950') & (`se_1960') & (`se_1970') \\"
            file write Table_results "\addlinespace[1ex]" _n
            
        }
        
    * Results by race
        file write Table_results "\addlinespace[1ex]" _n
        file write Table_results "\textit{By race:} & & & & & & & \\" 
        file write Table_results "\addlinespace[2ex]" _n
        
        foreach p in 1 2 { 

        if "`p'"=="1" {
            file write Table_results "White " 
        }
        if "`p'"=="2" {
            file write Table_results "Black " 
        }
        
            forval i=1910(10)1970 {

                reg `dep' `indep' if decade==`i' & race==`p' [pw=wgt_sex_race],  robust
                
                local coeff = _b[`indep']
                local number1: display %-09.2fc `coeff' 
                
                local numb = _se[`indep']
                local se_`i': display %-04.2fc `numb'
                display `se_`i''
                
                file write Table_results "& `number1' "

            }

            file write Table_results " \\" _n
            
            file write Table_results "& (`se_1910') & (`se_1920') & (`se_1930') & (`se_1940') & (`se_1950') & (`se_1960') & (`se_1970') \\"
            file write Table_results "\addlinespace[1ex]" _n
        
        }
            
    * Results for race x sex groups
        file write Table_results "\addlinespace[1ex]" _n
        file write Table_results "\textit{By subgroup:} & & & & & & & \\" 
        file write Table_results "\addlinespace[2ex]" _n

        foreach z in 1 2 3 4 { 

            if "`z'"=="1" {
                file write Table_results "White men " 
            }
            if "`z'"=="2" {
                file write Table_results "$+$ white women " 
            }
            if "`z'"=="3" {
                file write Table_results "$+$ Black men " 
            }
            if "`z'"=="4" {
                file write Table_results "$+$ Black women " 
            }
            
            if "`z'"=="1" {
                local cond "& race==1 & sex==1" 
            }
            if "`z'"=="2" {
                local cond "& race==1" 
            }
            if "`z'"=="3" {
                local cond "& (sex==1 | (race==1 & sex==2))" 
            }
            if "`z'"=="4" {
                local cond " " 
            }
            
            forval i=1910(10)1970 {

                reg `dep' `indep' if decade==`i' `cond' [pw=wgt_sex_race],   robust
                
                local coeff = _b[`indep']
                local number1: display %-09.2fc `coeff' 
                
                local numb = _se[`indep']
                local se_`i': display %-04.2fc `numb'
                display `se_`i''
                
                file write Table_results "& `number1' "

            }

            file write Table_results " \\" _n
            
            file write Table_results "& (`se_1910') & (`se_1920') & (`se_1930') & (`se_1940') & (`se_1950') & (`se_1960') & (`se_1970') \\"
            file write Table_results "\addlinespace[1ex]" _n
            
        }

        file write Table_results "\addlinespace[1ex]" _n
        file write Table_results "\bottomrule" _n
        file write Table_results "\end{tabularx}" _n
        file close Table_results

    }
